package blog.service.impl;


import blog.dao.ChapterCategoryDao;
import blog.dao.ChapterChapterCategoryDao;
import blog.dao.ChapterDao;
import blog.dao.ConfigDao;
import blog.pojo.Chapter;
import blog.pojo.ChapterCategory;
import blog.pojo.ChapterChapterCategory;
import blog.pojo.Config;
import blog.service.ChapterCategoryService;
import blog.util.Timestamp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ChapterCategoryServiceImpl implements ChapterCategoryService {

    @Autowired
    private ChapterCategoryDao chapterCategoryDao;

    @Autowired
    private ChapterChapterCategoryDao chapterChapterCategoryDao;

    @Autowired
    private ChapterDao chapterDao;

    @Autowired
    private ConfigDao configDao;

    @Override
    public List<ChapterCategory> getIndexList() throws Exception {
        List<ChapterCategory> returnList = new ArrayList<>();

        QueryWrapper configWrapper = new QueryWrapper<Config>().eq("name", Config.HOME_CATEGORY_NUMBER);
        Long categoryNumber = Long.valueOf(configDao.selectOne(configWrapper).getValue());

        QueryWrapper configWrapper1 = new QueryWrapper<Config>().eq("name", Config.HOME_CHAPTER_NUMBER);
        Long chapterNumber = Long.valueOf(configDao.selectOne(configWrapper).getValue());

        QueryWrapper wrapper = new QueryWrapper<ChapterCategory>()
                .orderByAsc("serial")
                .ne("pid", 0);
        List<ChapterCategory> list = chapterCategoryDao.selectList(wrapper);
        //只有当前有可用文章得分类才会展示在前台页面
        for (ChapterCategory chapterCategory : list) {
            QueryWrapper<ChapterChapterCategory> temp = new QueryWrapper<>(new ChapterChapterCategory().setChapterCategoryId(chapterCategory.getId()));
            List<ChapterChapterCategory> chapterChapterCategoryList = chapterChapterCategoryDao.selectList(temp);
            if(chapterChapterCategoryList.size() == 0) {
                continue;
            }
            List<Integer> chapterIdList = new ArrayList<>();
            for(ChapterChapterCategory chapterChapterCategory : chapterChapterCategoryList){
                chapterIdList.add(chapterChapterCategory.getChapterId());
            }

            QueryWrapper chapterWrapper = new QueryWrapper<Chapter>()
                    .setEntity(new Chapter().setIsuse(1))
                    .orderByAsc("serial")
                    .orderByDesc("gmt_create")
                    .in("id", chapterIdList)
                    .last("limit " + chapterNumber);
            List<Chapter> chapterList = chapterDao.selectList(chapterWrapper);
            if(chapterList.size() == 0) {
                continue;
            }
            chapterCategory.setChapterList(chapterList);
            returnList.add(chapterCategory);
        }

        return returnList;
    }

    @Override
    public Page<ChapterCategory> getPage(Page<ChapterCategory> page, ChapterCategory chapterCategory, String keywords) throws Exception {
        if (page == null) return null;
        QueryWrapper<ChapterCategory> queryWrapper = new QueryWrapper<>(chapterCategory)
                .ne("isuse", 2);
        if (keywords != null) {
            queryWrapper.like("name", keywords);
        }
        page = chapterCategoryDao.selectPage(page, queryWrapper);
        for (ChapterCategory category : page.getRecords()) {
            if (category.getPid() != 0) {
                category.setParent(chapterCategoryDao.selectById(category.getPid()));
            }
        }
        return page;
    }

    @Override
    public Boolean insert(ChapterCategory chapterCategory) throws Exception {
        chapterCategory.setGmtCreate(Timestamp.gen()).setGmtUpdate(Timestamp.gen());
        return chapterCategoryDao.insert(chapterCategory) > 0;
    }

    @Override
    public Boolean update(ChapterCategory chapterCategory) throws Exception {
        chapterCategory.setGmtUpdate(Timestamp.gen());
        return chapterCategoryDao.updateById(chapterCategory) > 0;
    }


    @Override
    public List<ChapterCategory> getCategoryList(Integer level) throws Exception {
        QueryWrapper wrapper = new QueryWrapper<ChapterCategory>().ne("isuse", 2);
        if (level == 1) {
            wrapper.setEntity(new ChapterCategory().setPid(0));
        } else if (level == 2) {
            wrapper.ne("pid", 0);
        } else {
            return new ArrayList<>();
        }
        return chapterCategoryDao.selectList(wrapper);
    }

    @Override
    public ChapterCategory get(ChapterCategory chapterCategory) throws Exception {
        chapterCategory = chapterCategoryDao.selectOne(new QueryWrapper<>(chapterCategory));
        if(chapterCategory.getPid() != 0) {
            chapterCategory.setParent(chapterCategoryDao.selectById(chapterCategory.getPid()));
        }
        return chapterCategory;
    }

    @Override
    public List<ChapterCategory> getAll() throws Exception {
        QueryWrapper wrapper = new QueryWrapper<ChapterCategory>().ne("isuse", 2);
        List<ChapterCategory> chapterCategories = chapterCategoryDao.selectList(wrapper);
        for(ChapterCategory chapterCategory : chapterCategories){
            if(chapterCategory.getPid() != 0){
                chapterCategory.setParent(chapterCategoryDao.selectById(chapterCategory.getPid()));
            }
        }
        return chapterCategories;
    }

    @Override
    public List<ChapterCategory> getListByChapterId(Integer id) throws Exception {
        List<ChapterChapterCategory> chapterChapterCategoryList = chapterChapterCategoryDao.selectList(new QueryWrapper<>(new ChapterChapterCategory().setChapterId(id)));
        List<ChapterCategory> chapterCategoryList = new ArrayList<>();
        for(ChapterChapterCategory chapterChapterCategory : chapterChapterCategoryList){
            chapterCategoryList.add(chapterCategoryDao.selectById(chapterChapterCategory.getChapterCategoryId()));
        }
        return chapterCategoryList;
    }

    @Override
    public Integer selectCountByPid(Integer pid) throws Exception {
        return chapterCategoryDao.selectCount(new QueryWrapper<>(new ChapterCategory().setPid(pid).setIsuse(1)));
    }
}
